<template>
  <div class="mainNav" id="mainNav">
    <div class="box_center cf">
      <ul class="nav" id="navModule">
        <li><a @click="goHome">首页</a></li>
        <li>
          <a> 全部作品 </a>
        </li>
        <li><a>排行榜</a></li>
        <li class=""><a @click="goRecharge">充值</a></li>
        <li><a @click="goAuthor" href="javascript:void(0)">作家专区</a></li>
      </ul>
    </div>
  </div>
</template>
<script setup>
import { useRouter, useRoute } from 'vue-router'
import { getToken } from '@/utils/auth'
import { getAuthorStatus } from '@/api/author'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const goAuthor = async () => {
  //如果未登录会员则跳转登录页面
  if (!getToken()) {
    router.push({
      name: 'login',
    })
    return
  }
  //已登录 查询作家状态是否已经注册过 作家
  const { data: res } = await getAuthorStatus()

  //如果未注册则跳转到作家注册页面
  if (res.data === null) {
    //为空未将会员号与作家绑定注册
    router.push({
      name: 'authorRegister',
    })
    return
  } else {
    //已绑定跳转到 作家小说列表
    let routeUrl = router.resolve({
      name: 'authorBookList',
    })
    window.open(routeUrl.href, '_blank')
  }
}

const goHome = () => {
  router.push({
    name: 'home',
  })
}

const goRecharge = async () => {
  if (!getToken()) {
    ElMessage({
      message: '请登录后再充值',
      type: 'warning',
    })

    router.push({
      name: 'login',
    })
    return
  } else {
    router.push({
      name: 'recharge',
    })
  }
}
</script>

<style scoped>
.nav li a {
  cursor: pointer;
}
</style>
